<?php

namespace app\common\model;

class Post extends \think\Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $append = [
        'is_like',
       'image_array',
       'video_array'
    ];

    /**
     * 是否点赞
     * @param string $value
     * @return string
     */
    public function getIsLikeAttr($value,$data)
    {//是否点赞方法未实现
        return Likes::isLike($data['id']);
    }
    public function getImageArrayAttr($value,$data)
    {
        return explode(',',$data['images']);
    }
    public function getVideoArrayAttr($value,$data)
    {
        return explode(',',$data['videofiles']);
    }

    public function scopeShow($query)
    {
        $query->where('status','in','1,2');
    }
    public function scopeHot($query)
    {
        $query->where('switch',1);
    }

    public function user(){
        return $this->belongsTo(User::class)->field('id,nickname,avatar');
    }
    public function topic(){
        return $this->hasOne(Topic::class,'id','topic_ids')->field('id,title,image');
    }
    public function category(){
        return $this->hasOne(Category::class,'id','category_id')->field('id,type,name,image,flag');
    }

}